/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.bean;

import desu.nya.DAO.AnimeFactory;
import desu.nya.Otaku;
import desu.nya.Rating;
import java.sql.SQLException;
import javax.annotation.ManagedBean;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Fess
 */
@ManagedBean
@SessionScoped
public class SessBean {
    private String log;
    private String pass="";
    private Otaku currentOtaku=null;
    private Long animeId=-1l;
    private int rateInt;
    private Rating currRate=null;
    private org.primefaces.component.rating.Rating rateBind;

    public org.primefaces.component.rating.Rating getRateBind() {
        return rateBind;
    }

    public void setRateBind(org.primefaces.component.rating.Rating rateBind) {
        this.rateBind = rateBind;
    }
    
    public int getRateInt() {
        return rateInt;
    }

    public void setRateInt(int rateInt) {
        this.rateInt = rateInt;
    }
    
    public String getLog() {
        return log;
    }

    public String getPass() {
        return pass;
    }

    public void setLog(String log) {
        this.log = log;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    
    public void logon(){
        AnimeFactory factory=AnimeFactory.getInstance();
        currentOtaku=factory.getOtakuDAO().login(log, pass);
        pass="";
        if(currentOtaku==null){
            FacesContext context=FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Login fail","Login or Pass invalid"));
        }
    }
    
    public boolean isAuth(){
        return currentOtaku!=null;
    }
    
    public String getCurrNick(){
        return currentOtaku==null?null:currentOtaku.getNickname();
    }
    
    public int getRate(){
        AnimeFactory factory=AnimeFactory.getInstance();
        int r=-1;
        try{
            String s=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("animeId");
            animeId=Long.parseLong(s);
            currRate=factory.getAnimeDAO().getRating(animeId, currentOtaku.getNickname());
            if(currRate!=null)r=currRate.getRate();
        } catch (SQLException ex) {
        } catch(Exception ex){
            System.err.println("Get rating failed");
        }
        rateInt=r;
        return r;
    }
    
    public void rateChange(){
        AnimeFactory factory=AnimeFactory.getInstance();
        currRate.setRate(rateInt);
        try {
            factory.getRatingDAO().updateRating(currRate.getId(), currRate);
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
        }
        System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
        
    }
    
    public void testing(){
        String str=Long.toString(100550l);
        System.err.print(str);
    }
}
